package com.atguigu.spzx.manager.controller;


import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import com.atguigu.spzx.utils.AuthContextUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// swagger 注解
@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
//@CrossOrigin(allowCredentials = "true",originPatterns = "*",allowedHeaders = "*")
public class IndexController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ValidateService validateService;
    @Autowired
    private SysMenuService sysMenuService;
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "登录的方法")
    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    //    @GetMapping("/getUserInfo")
//    public Result getUserInfo(@RequestHeader(name = "token") String token) {
////        1.从请求头获取token
////             方式1：        request.getHeader("token")
////        2.根据token查询redis获取用户信息
//        SysUser sysUser = sysUserService.getUserInfo(token);
////        3.用户信息返回
//        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
//    }
//    优化 直接从LocalThread获取
    @Operation(summary = "从ThreadLocal对象获取用户信息")
    @GetMapping("/getUserInfo")
    public Result getUserInfo() {
        return Result.build(AuthContextUtils.get(), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "退出接口")
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(name = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询用户可以操纵的菜单")
    @GetMapping("/menus")
    public Result menus(){
        List<SysMenuVo> sysMenuVoList = sysMenuService.findUserMenuList();
        return Result.build(sysMenuVoList,ResultCodeEnum.SUCCESS);
    }

}
